<template>
  <div>
    <!-- 顶部 -->
    <van-nav-bar title="购买" left-arrow @click-left="goBack"></van-nav-bar>
    <div class="buy1">剩余可认购额度 {{canMore}} {{canName}}</div>
    <div class="buy2">
      <span>可用余额</span>
      <span>{{canManey}} {{canName}}</span>
      <span @click="$router.push({name:'recharge'})">充值</span>
    </div>
    <div class="buy3">
      <div>认购数量</div>
      <input type="text" style="color:#999" v-model="canIn" name id :placeholder="canBuy">
    </div>
    <div class="buy4">
      <p>
        <span>预期收益</span>
        <span>0 {{canName}}</span>
      </p>
      <p>
        <span>预期年化收益</span>
        <span>{{canGet}}%</span>
      </p>
      <p>
        <span>预期收款日</span>
        <span style="color:#999">{{canTime}}</span>
      </p>
    </div>
    <div class="buy5">*收益自动转入活期产品余额</div>

    <div class="buy6">
      <van-radio-group v-model="radio" class="buy6-one">
        <van-radio name="1">
          勾选即表示您已阅读并同意我们的
          <span style="color:#D1A568">《投资服务协议》</span>
        </van-radio>
      </van-radio-group>
      <div class="buy6-two" @click="clickGo">确认</div>
    </div>

    <!-- 弹框交易密码认证 -->
    <div class="cover" v-if="flagCover">
      <div class="cover-contain">
        <p style="text-align:center;font-weight:bold;font-size:16px;">请输入交易密码</p>
        <p>
          <input v-model="myPass" type="text" placeholder="请输入交易密码" name id>
        </p>
        <p style="font-size:10px;" @click="goToSet">忘记密码</p>
        <div class="submit">
          <button @click="checkPass" style="margin-top:0" type="submit" class="submit-btn">确定</button>
        </div>
        <img @click="flagCover=false" id="close" src="../../../assets/images/close.png" alt>
      </div>
    </div>

    <!-- 购买成功的页面 -->
    <div class="buyCuccess" v-if="flagContinue">
      <p>您购买的{{canName}} {{canAllDay}}日抢先计划{{productName}}</p>
      <p>祝您投资愉快</p>
      <div>
        <img src="../../../assets/images/sucess.png" alt>
      </div>
      <div class="buy6-two" @click="continueGo">继续预览</div>
    </div>
  </div>
</template>
<script>
import { NavBar, Toast, Tab, Tabs, RadioGroup, Radio } from "vant";
import {
  coinAssets,
  addBuy,
  checkPassword,
  errorCount
} from "../../../axios/api.js";
export default {
  components: {
    [NavBar.name]: NavBar,
    [Toast.name]: Toast,
    [Tab.name]: Tab,
    [Tabs.name]: Tabs,
    [RadioGroup.name]: RadioGroup,
    [Radio.name]: Radio
  },
  data() {
    return {
      radio: "0",
      flagCover: false,
      canMore: this.$route.query.name5,
      canBuy: this.$route.query.name1 + this.$route.query.name4 + "起购",
      canGet: this.$route.query.name2,
      canTime: this.$route.query.name3,
      canName: this.$route.query.name4,
      canAllDay: this.$route.query.name6,
      productName: this.$route.query.productName,
      canManey: "0.00",
      canIn: "",
      canBuyTo: this.$route.query.name1,
      myPass: "",
      flagContinue: false
    };
  },
  created() {
    this.getCoinAssets();
  },
  methods: {
    //返回上一页
    goBack() {
      this.$router.go(-1);
    },
    //忘记交易密码
    goToSet() {
      this.$router.push({ name: "passwordSetting" });
    },
    //继续预览
    continueGo() {
      this.$router.push({ name: "product" });
    },
    //获取可用余额
    getCoinAssets() {
      coinAssets({
        params: {
          token: localStorage.getItem("setToken"),
          virtualWalletId: this.$route.query.id
        }
      }).then(res => {
        console.log(res.data);
        this.canManey = res.data.data.coinAssetc;
      });
    },
    //确认
    clickGo() {
      var that = this;
      console.log(that.canIn);
      if (that.canIn == "" || that.canIn - that.canBuyTo < 0) {
        Toast("认购数量太少了！");
        return false;
      }
      if (that.canIn - that.canMore > 0) {
        Toast("认购数量太多了！");
        return false;
      }
      if (that.radio == "0") {
        Toast("未同意投资服务协议！");
        return false;
      }

      errorCount({
        params: {
          token: localStorage.getItem("setToken")
        }
      }).then(res => {
        console.log(res.data, 999);
        if (res.data.code == 200) {
          that.flagCover = true;
        } else {
          Toast(res.data.msg);
          return false;
        }
      });
    },
    //校验交易密码
    checkPass() {
      var that = this;
      if (that.myPass == "") {
        Toast("请输入交易密码！");
        return false;
      }
      if(!/^\d{6}$/.test(that.myPass)){
         Toast('交易密码格式有误！')
        return false; 
      }
      checkPassword({
        params: {
          token: localStorage.getItem("setToken"),
          tradePassword: that.myPass
        }
      }).then(res => {
        console.log(res.data);
        if (res.data.code == 200) {
          that.buyProduct();
        } else {
          Toast('交易密码错误！')
          that.flagCover = false;
        }
      });
    },
    //购买
    buyProduct() {
      var that = this;
      $.ajax({
        type: "post",
        dataType: "json",
        url: window.nowHref + "/userCoinPlan/add",
        data: {
          token: localStorage.getItem("setToken"),
          coinPlanId: that.$route.query.name8,
          lockedDays: that.$route.query.name6,
          remainingAmount: that.$route.query.name5,
          investmentAmount: that.canIn
        },
        success: function(data) {
          console.log(data);
          if (data.code == 200) {
            that.flagContinue = true;
            that.flagCover = false;
          }
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
//购买成功页面
.buyCuccess {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: #fff;
  p:nth-child(1) {
    margin-top: 15vh;
    font-size: 18px;
    color: #333;
  }
  p:nth-child(2) {
    margin-top: 10px;
    font-size: 16px;
    color: #333;
  }
  div:nth-child(3) {
    margin: 50px auto;
    width: 30%;
    img {
      width: 100%;
    }
  }
}
.buy1 {
  font-size: 14px;
  padding: 12px 20px;
  box-sizing: border-box;
  text-align: left;
  color: #d1a568;
}
.buy2 {
  text-align: left;
  padding: 0 20px 12px;
  border-bottom: 1px solid #eeeeee;
  box-sizing: border-box;
  span:nth-child(1) {
    font-size: 16px;
    color: #333;
    display: inline-block;
    width: 30%;
  }
  span:nth-child(2) {
    display: inline-block;
    width: 45%;
    font-size: 14px;
    color: #333;
  }
  span:nth-child(3) {
    display: inline-block;
    width: 20%;
    font-size: 16px;
    color: #d1a568;
    text-align: right;
  }
}
.buy3 {
  text-align: left;
  padding: 12px 20px;
  border-bottom: 1px solid #eeeeee;
  box-sizing: border-box;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  > div:nth-child(1) {
    width: 30%;
    font-size: 16px;
    color: #333;
  }
  > input {
    border: none;
    font-size: 14px;
  }
}
.buy4 {
  text-align: left;
  padding: 12px 20px;
  border-bottom: 10px solid #eeeeee;
  box-sizing: border-box;
  p {
    font-size: 12px;
    color: #666;
    margin-bottom: 6px;
    span:nth-child(1) {
      display: inline-block;
      width: 30%;
    }
    span:nth-child(2) {
      color: #d1a568;
    }
  }
}
.buy5 {
  text-align: left;
  padding: 12px 20px;
  box-sizing: border-box;
  border-bottom: 10px solid #eeeeee;
  font-size: 14px;
  color: #333;
}
.buy6-one {
  margin-top: 10vh;
  font-size: 12px;
  color: #999;
}
.buy6-two {
  width: 90%;
  margin: 20px auto;
  height: 40px;
  line-height: 40px;
  color: #fff;
  border-radius: 20px;
  background-color: #d1a568;
  font-size: 18px;
}

.cover {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  background-color: rgba(0, 0, 0, 0.4);
  z-index: 99;
  font-size: 14px;
  .cover-contain {
    width: 80%;
    background-color: #fff;
    border-radius: 5px;
    margin: 30vh auto;
    padding: 20px 20px 20px;
    box-sizing: border-box;
    position: relative;
    #close {
      position: absolute;
      right: 7px;
      top: 4px;
      width: 7%;
    }
    p {
      text-align: left;
      margin-bottom: 20px;
      input {
        border: none;
      }
    }
  }
}
.detail {
  width: 90%;
  margin: 30px auto;
  text-align: left;
  font-size: 9px;
  color: #999;
  p:first-child {
    font-size: 12px;
  }
}

.submit {
  margin-top: 0.67rem;
  width: 100%;
  background-color: #fff;
  color: #fff;
  font-size: 14px;
  .submit-btn {
    width: 85%;
    height: 34px;
    text-align: center;
    line-height: 34px;
    font-size: 14px;
    background-color: #d6ab6f;
    color: white;
    border-radius: 20px;
    margin: 10vh auto 0;
    border: 0;
  }
}
</style>
